package com.springboot.springbootweb.jsoup;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class pachong_konachan {


    //https://konachan.com/post?tags=go-toubun_no_hanayome
    public static void main(String[] args) throws Exception {
        int end = 20;
        for (int page = 1; page < end; page++) {
            String url = "https://konachan.com/post?page=" + page + "&tags=nude";
            String urlimage = "https://konachan.com";
            //链接到目标地址
            Connection connect = Jsoup.connect(url);
            //设置useragent,设置超时时间，并以get请求方式请求服务器
            Document document = connect.userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").timeout(6000).ignoreContentType(true).get();
            Thread.sleep(1000);
            //获取指定标签的数据
            Elements elements = document.getElementsByClass("thumb");
            for (Element e : elements) {
                //size++;
                //System.out.println(e);
                //System.out.println("-----");
                //System.out.println(e.getElementsByAttribute("href").attr("href"));//获取src
                //链接到目标地址
                Connection connectimage = Jsoup.connect(urlimage + e.getElementsByAttribute("href").attr("href"));
                //设置useragent,设置超时时间，并以get请求方式请求服务器
                Document documentimage = connectimage.userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").timeout(6000).ignoreContentType(true).get();
                Thread.sleep(1000);
                //获取指定标签的数据H:\壁纸\nude
                Element elementsimage = documentimage.getElementById("image");
                //System.out.println("H:\\壁纸\\nude\\"+(page+"_"+size)+".jpg");
                //System.out.println("-----");
                //System.out.println(elementsimage.getElementsByAttribute("src").attr("src"));//获取src
                Elements eles = documentimage.getElementsByTag("title");
                Element element = eles.get(0);
                String filename = element.text().replace(" ", "");
                filename = filename.replace("|", "");
                filename = filename.replace(">", "");
                filename = filename.replace("<", "");
                filename = filename.replace("\"", "");
                filename = filename.replace("?", "");
                filename = filename.replace("*", "");
                filename = filename.replace(":", "");
                filename = filename.replace("/", "");
                filename = filename.replace("\\", "");
                filename = filename.replace(".", "");
                filename = filename.replace("-", "");
                filename = filename.replace("!", "");
                if (filename.length() > 90) {
                    filename = filename.substring(filename.length() - 90, filename.length() - 40);
                }
                System.out.println("标题:" + filename);
                File localFile = new File("G:\\壁纸\\" + filename + ".jpg");
                if (!localFile.exists()) {
                    downloadImage(elementsimage.getElementsByAttribute("src").attr("src"), "jpg", localFile);
                }
            }
        }
        //输出文本数据
        //System.out.println(elements.text());
        //输出html数据
    }

    /**
     * 网络图片下载
     *
     * @param url        图片url
     * @param formatName 文件格式名称
     * @param localFile  下载到本地文件
     * @return 下载是否成功
     */
    public static boolean downloadImage(String imageUrl, String formatName, File localFile) {
        boolean isSuccess = false;
        URL url = null;
        try {
            url = new URL(imageUrl);
            isSuccess = ImageIO.write(ImageIO.read(url), formatName, localFile);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isSuccess;

    }
}
